<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      lock-scroll
      width="800px"
      @close="handleClose"
    >
      <div class="dialog_div_new">
        <el-form :model="newData" :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-row :gutter="24" style>
            <el-col :span="12">
              <el-form-item label="仓库名称" prop="storehouseName">
                <el-input v-model="newData.storehouseName" @blur="getNameToPinying"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="拼音码" prop="companyUserName">
                <el-input v-model="newData.storehousePinyinCode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24" style>
            <el-col :span="12">
              <el-form-item label="仓库类别" prop="companyName">
                <div class="custom_select_head">
                  <el-select
                    v-model="newData.storehouseTypeId"
                    style="width:100%;"
                    filterable
                    :filter-method="filterMethod"
                    @visible-change="visibleChange"
                  >
                    <el-option
                      v-for="item in WarehouseTypeOptions"
                      :key="item.id"
                      :label="item.storehouseTypeName"
                      :value="item.id"
                    ></el-option>
                    <el-option key label value>
                      <div class="custom_add" @click.stop="onShowWarehouseAdd">
                        <i class="el-icon-plus"></i>
                        <span>新增</span>
                      </div>
                    </el-option>
                  </el-select>
                  <div class="custom_more">
                    <i class="el-icon-more" @click.stop="onShowTree"></i>
                  </div>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="仓库编码" prop="storehouseCode">
                <el-input v-model="newData.storehouseCode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input type="textarea" v-model="newData.remark" :autosize="{ minRows: 5}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item class="bottom_item">
                <el-button type="primary" @click.stop="onAddSubmit" style="margin-right:20px;">保存</el-button>
                <el-button @click.stop="dialogVisible = false" style="margin-left:20px;">取消</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>
    <!-- 选择类别 -->
    <dialogTreeStore ref="treeRef" :toTreeData="toTreeData" @onSelectTree="onSelectTree"></dialogTreeStore>
    <!-- 提醒弹框 -->
    <PopupReminderNew ref="reminderRef" :tipsData="tipsData" @dialogVisibleClose="onNo"></PopupReminderNew>
    <!-- 新增树形结构数据弹框 -->
    <el-dialog title="类别信息" :visible.sync="showNewTree" width="480px">
      <NewCategory
        :newTreeData="newTreeData"
        @onSaveTreeChanges="onSaveTreeChanges"
        @onCancelTree="showNewTree = false"
      ></NewCategory>
    </el-dialog>
  </div>
</template>

<script>
import dialogTreeStore from './dialogTreeStore'
import PopupReminderNew from './popupReminderNew'
import { postAction, getAction, httpAction } from '@/api/manage'
import NewCategory from '@/components/c_category/NewCategory'
export default {
  props: {
    newData: {
      type: Object,
      default: () => ({
        storehouseName: '',
        storehousePinyinCode: '',
        storehouseTypeId: '',
        storehouseTypeName: '',
        storehouseCode: '',
        remark: '',
        isMain: false,
      })
    },
    title: {
      type: String,
      default: '新增仓库',
    },
  },
  components: {
    dialogTreeStore,
    PopupReminderNew,
    NewCategory
  },
  created () { },
  // watch: {
  //   newData() {
  //     this.newData.isMain = this.newData.isMain === 1 ? true : false
  //     console.log(this.newData, 'this.newData')
  //   },
  // },
  data () {
    return {
      // 传递给弹框的数据
      tipsData: {
        tipsState: '',
        tipsWords: '',
        tipsMark: '',
      },
      // 传递给选择类别的数据
      toTreeData: {
        module: {
          name: 'storehouseTypeName', //类别名称字段
          code: 'storehouseCode', //类别编码字段
          pyCode: 'pinyinCode', //类别拼音码字段
          indexCode: 'indexCode', //类别排序码字段
          pid: 'pid'
        },
        getTreeUrl: '/xghStorehouseType/xghStorehouseType/typeList', //获取类别接口
        deleteTreeUrl: '/xghStorehouseType/xghStorehouseType/delete', //删除类别接口
        addTreeUrl: '/xghStorehouseType/xghStorehouseType/add', //新增类别接口
        editTreeUrl: '/xghStorehouseType/xghStorehouseType/edit', //修改类别接口
      },
      // 是否默认仓库
      checked: false,
      // newData: {
      //   storehouseName: '',
      //   storehousePinyinCode: '',
      //   storehouseTypeId: '',
      //   storehouseCode: '',
      //   remark: '',
      //   isMain: 0,
      // },
      WarehouseTypeOptions: [
        {
          value: '主仓库',
          label: '主仓库',
        },
        {
          value: '门店仓库',
          label: '门店仓库',
        },
        {
          value: '分仓库一',
          label: '分仓库一',
        },
      ],
      dialogVisible: false,
      activeOptions: [],
      showNewTree: false,
      // 传递数据给新增树形结构组件
      newTreeData: {
        parentNode: '',
        nowNode: '',
      },
      // 必填校验
      rules: {
        storehouseName: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        // storehouseCode: [{ required: true, message: '请输入仓库编码', trigger: 'blur' }],
      },
    }
  },
  watch: {
    dialogVisible (val) {
      if (val == true) {
        this.getTreeData()
      }
    }
  },
  methods: {
    // 获取树形结构类别数据
    getTreeData () {
      getAction('/xghStorehouseType/xghStorehouseType/typeList', {}).then((res) => {
        if (res.code === 200) {
          this.WarehouseTypeOptions = res.result
        }
      })
    },
    onShowWarehouseAdd () {
      this.newTreeData = {
        data: { id: '0' },
        state: 'add',
        module: {
          name: 'storehouseTypeName', //类别名称字段
          code: 'storehouseCode', //类别编码字段
          pyCode: 'pinyinCode', //类别拼音码字段
          indexCode: 'indexCode', //类别排序码字段
        },
        getTreeUrl: '/xghStorehouseType/xghStorehouseType/typeList', //获取类别接口
        deleteTreeUrl: '/xghStorehouseType/xghStorehouseType/delete', //删除类别接口
        addTreeUrl: '/xghStorehouseType/xghStorehouseType/add', //新增类别接口
        editTreeUrl: '/xghStorehouseType/xghStorehouseType/edit', //修改类别接口
      }
      this.showNewTree = true
    },
    // 类别信息新增/修改后数据获取
    onSaveTreeChanges () {
      this.getTreeData()
      this.showNewTree = false
    },
    filterMethod (e) {
      if (this.activeOptions.length == 0) {
        this.activeOptions = Object.assign([], this.userOptions)
      }
      if (e) {
        this.WarehouseTypeOptions = this.activeOptions.filter(item => {
          return item.realname.indexOf(e) > -1
        })
      } else {
        this.WarehouseTypeOptions = Object.assign([], this.activeOptions)
      }
    },
    visibleChange (e) {
      if (!e) {
        this.activeOptions = []
      }
    },
    handleClose () {
      // this.newData = {
      //   storehouseName: '',
      //   storehousePinyinCode: '',
      //   storehouseTypeId: '',
      //   storehouseTypeName: '',
      //   storehouseCode: '',
      //   remark: '',
      //   isMain: 0,
      // }
      this.$emit('handleClose')
    },
    // 提醒弹框确认后操作
    onNo (state) {
      if (!state) {
        this.newData.isMain = !this.newData.isMain
      }
    },
    handleChange () {
      this.tipsData.tipsWords = '是否要更改默认仓库?'
      this.tipsData.tipsMark = 'handleChange'
      this.$refs.reminderRef.dialogVisible = true
    },
    // 选择当前类别
    onSelectTree (val) {
      this.newData.storehouseTypeName = val.storehouseTypeName
      this.newData.storehouseTypeId = val.id
      this.$forceUpdate()
    },
    // 打开选择类别弹出框
    onShowTree () {
      this.$refs.treeRef.getTreeData()
      this.$refs.treeRef.dialogVisible = true
    },
    // 转换拼音码
    getNameToPinying (event) {
      this.newData.storehousePinyinCode = this.$pinyin.getJp(event.target.value)
      this.$forceUpdate()
    },
    onAddSubmit () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.dialogVisible = !this.dialogVisible
          this.$emit('onAddSubmit', this.newData)
        }
      });
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__inner {
  height: 30px;
  line-height: 30px;
}

::v-deep .cus_div .el-input__inner {
  padding: 0 0 0 5px;
}
.dialog_div_new {
  padding: 12px 30px;
  // height: 60vh;
  overflow: auto;
  background: #fff;
}
.custom_select_head {
  // right: 10px;
  width: 100%;
  height: 100%;
  position: relative;
}
.custom_more {
  position: absolute;
  right: 10px;
  z-index: 10;
  top: 6px;
  background: #fff;
  height: 28px;
  display: flex;
  align-items: center;
  color: #bfbfbf;
}
.custom_add {
  display: flex;
  justify-content: center;
  align-items: center;
  span {
    margin-left: 5px;
    color: #666666;
  }
  i {
    color: #666666;
  }
}
.cus_div {
  display: flex;
  align-items: center;
  margin-left: 5px;
}

.bottom_item {
  margin: 30px 0;
  width: 100%;
  text-align: center;
}
::v-deep .el-form-item {
  display: flex;
  width: 100%;
  margin: 10px 0;

  .el-form-item__label {
    width: auto;
    min-width: 70px;
    line-height: 40px;
    font-size: 12px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #666;
    text-align: justify;
    text-justify: distribute-all-lines; /*ie6-8*/
    text-align-last: justify; /* ie9*/
    -moz-text-align-last: justify; /*ff*/
    -webkit-text-align-last: justify; /*chrome 20+*/
    position: relative;
    &::before {
      position: absolute;
      right: 0;
      margin-right: 0;
      color: #ff0000;
      font-weight: 600;
    }
  }
  .el-form-item__content {
    flex: 1;
  }
}
</style>
